﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbSharp.Framework.ReleaseManage;
using DbSharp.Framework.Driver;
using System.Data.Common;

namespace DbSharp.Framework.TestEngine
{
    public class RuntimeInfo
    {

        #region Private members
        private EditionFuncControlOption m_EditionOption;
        private object m_InterruptLock = new object();
        private bool m_Interrupted;

        private object m_TestInProcessLock = new object();
        private bool m_TestInProcess;
        private TestDataSourceFactory m_TestDataSourceFactory = new TestDataSourceFactory();
        #endregion


        #region Singleton pattern

        readonly static RuntimeInfo m_Instance = new RuntimeInfo();

        private void CreateDefaultEditionOption()
        {
            if (m_EditionOption == null)
                m_EditionOption = EditionOptionFactory.CreateDefaultEditionOption();
        }

        private RuntimeInfo()
        {
           
        }

        public static RuntimeInfo GetInstance()
        {
            return m_Instance;
        }

        #endregion

        public TestDataSourceFactory TestDataSourceFactory
        {
            get { return m_TestDataSourceFactory; }
        }


        public bool TestInProcess
        {
            get
            {
                lock (m_TestInProcessLock)
                {
                    return m_TestInProcess;
                }
            }
            set
            {
                lock (m_TestInProcessLock)
                    m_TestInProcess = value;
            }
        }

        public bool Interrupted
        {
            get
            {
                lock (m_InterruptLock)
                {
                    return m_Interrupted;
                }
            }
            set
            {
                lock (m_InterruptLock)
                    m_Interrupted = value;
            }
        }

        public EditionFuncControlOption SoftwareEditionOption
        {
            get { return m_EditionOption; }
            set { m_EditionOption = value; }
        }

        public bool IsSoftwareExpired(ITestDataSource testDb, DbConnection dbConn)
        {
            CreateDefaultEditionOption();
            return m_EditionOption.IsExpired(testDb, dbConn);
        }

        public void SetExpiredFlag()
        {
            m_EditionOption.SetEvaluationExpired();
        }

        public bool IsValidLicense()
        {
            CreateDefaultEditionOption();
            return m_EditionOption.IsValidLicense();
        }


        public bool IsDbConnCfgCountOverflow(int connectionCfgCount)
        {
            CreateDefaultEditionOption();
            return m_EditionOption.IsDbConnCfgCountOverflow(connectionCfgCount);
        }

        public bool IsPerfCaseCountOverflow(int perfCaseCount)
        {
            CreateDefaultEditionOption();
            return m_EditionOption.IsPerfCaseCountOverflow(perfCaseCount);
        }

        public bool IsUnitCaseCountOverflow(int unitCaseCount)
        {
            CreateDefaultEditionOption();
            return m_EditionOption.IsUnitCaseCountOverflow(unitCaseCount);
        }

        public int MaxDbConnConfigCount
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.MaxDbConnConfigCount;
            }
        }

        public int MaxPerfTestCaseCount
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.MaxPerfTestCaseCount;
            }
        }

        public int MaxUnitTestCaseCount
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.MaxUnitTestCaseCount;
            }
        }

        public bool AdvancedDiffEnabled
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.AdvancedDiffEnabled;
            }
        }

        public bool TestInSandboxSupported
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.TestInSandboxEnabled;
            }
        }

        public bool SetupTeardownSupported
        {
            get
            {
                CreateDefaultEditionOption();
                return m_EditionOption.UnitTestSetupEnabled;
            }
        }
    }
}
